package client.view;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import javax.swing.*;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * 登录窗口
 */
public class LoginView extends Application {
    private String host;
    private int port;
    private TextField serverAddressText;
    private TextField portText;
    private TextField userNameText;
    private Button btn;

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Le Chat Login");
        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(25, 25, 25, 25));
        ImageView loginImage = new ImageView("client/view/image/login.png");
        loginImage.setFitWidth(330);
        loginImage.setFitHeight(140);
        grid.add(loginImage, 0, 0, 2, 1);

        Label serverAddress = new Label("Server address:");
        serverAddress.setStyle("-fx-font-size: 15px");
        grid.add(serverAddress, 0, 1);

        serverAddressText = new TextField("localhost");
        serverAddressText.setStyle("-fx-font-size: 15px");
        grid.add(serverAddressText, 1, 1);

        Label port = new Label("Server Port:");
        port.setStyle("-fx-font-size: 15px");
        grid.add(port, 0, 2);

        portText = new TextField("7777");
        portText.setStyle("-fx-font-size: 15px");
        grid.add(portText, 1, 2);

        Label userName = new Label("locaUsername:");
        grid.add(userName, 0, 3);
        userName.setStyle("-fx-font-size: 15px");

        userNameText = new TextField();
        userNameText.setPromptText("user name");
        userNameText.setStyle("-fx-font-size: 15px");
        grid.add(userNameText, 1, 3);

        Label label = new Label();
        grid.add(label, 0, 4);
        label.setStyle("-fx-font-size: 15px");
        label.setTextFill(Color.RED);

        Button btn = new Button("Login");
        btn.setMinWidth(130);
        btn.setMinHeight(35);
        HBox hbBtn = new HBox(30);
        hbBtn.setStyle("-fx-font-size: 16");
        btn.setStyle("-fx-background-color: #50b4ea");
        hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
        hbBtn.getChildren().add(btn);
        grid.add(hbBtn, 1, 4);

        Scene scene = new Scene(grid, 380, 325);
        primaryStage.setScene(scene);
        primaryStage.getIcons().add(new Image("client/view/icon.png", 256, 256, true, true));
        primaryStage.show();
        btn.setOnAction((event) -> {
            try {
                String inputAddress = serverAddressText.getText();
                int inputPort = Integer.parseInt(portText.getText());
                Socket socket = new Socket(inputAddress, inputPort);
                ObjectOutputStream toServer = new ObjectOutputStream(socket.getOutputStream());
                ObjectInputStream  fromServer=new ObjectInputStream(socket.getInputStream());
                String yourInputName = userNameText.getText();
                if(yourInputName.equals("")){
                    JOptionPane.showMessageDialog(null,"用户名不能为空！","错误提示",JOptionPane.ERROR_MESSAGE);
                    return;
                }
                toServer.writeObject(yourInputName);
                String loginResult=fromServer.readObject().toString();
                if(loginResult.equals("success")){
                    ChatView chatView = new ChatView(yourInputName, inputAddress, inputPort, socket, toServer,fromServer);
                    chatView.start(new Stage());
                    btn.getScene().getWindow().hide();
                }else{
                    JOptionPane.showMessageDialog(null,"该用户已经登录,请重新输入用户名！","错误提示",JOptionPane.ERROR_MESSAGE);
                    userNameText.setText("");
                    System.out.println("登陆失败");
                }
            } catch (Exception e) {
                label.setText("该用户已登录,请重新输入用户名");
                userNameText.setText("");
            }
        });
    }

    public static void main(String[] args) {
        launch(args);
    }


}
